package org.onvivals.admin.module.pms.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.onvivals.common.param.PageParam;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.pms.entity.PmsRole;
import org.onvivals.module.pms.param.PmsRoleParam;
import org.onvivals.module.pms.service.IPmsRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.onvivals.module.ums.entity.UmsUser;
import org.onvivals.module.ums.service.IUmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/pms/role")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PmsRoleController {

    private final IPmsRoleService roleService;

	private final IUmsUserService userService;

    @Operation(summary = "分页查询全部角色")
    @GetMapping("/page")
    public Result<PageResult<PmsRole>> page(PmsRoleParam param) {
		PageParam<PmsRole, PmsRoleParam> page = new PageParam<>(param);
		page.setDefaultOrder("create_time desc");
		IPage<PmsRole> pa = roleService.page(page, page.getWrapper());
		List<PmsRole> records = pa.getRecords();
		records.forEach(k->{
			if (ObjectUtil.isNotEmpty(k.getUid())){
				UmsUser user = userService.getById(k.getUid());
				if (user!=null){
					k.setUmsUser(user);
				}
			}
		});
		PageResult<PmsRole> pageResult = new PageResult<>(pa.getRecords(), pa.getTotal());
		return Result.OK("查询成功", pageResult);
    }

    @Operation(summary = "查询全部角色")
    @GetMapping()
    public Result<List<PmsRole>> list(PmsRoleParam param) {
        PageParam<PmsRole, PmsRoleParam> page = new PageParam<>(param);
        List<PmsRole> pmsLlmConfigList = roleService.list(page.getOrderWrapper());
        return Result.OK("查询成功", pmsLlmConfigList);
    }

    @Operation(summary = "根据id查询角色")
    @GetMapping("/{id}")
    public Result<PmsRole> get(@PathVariable("id") Long id) {
        PmsRole PmsRole = roleService.getById(id);
        return Result.OK("查询成功", PmsRole);
    }

    @Operation(summary = "添加角色")
    @PostMapping()
    public Result<?> save(@RequestBody PmsRole PmsRole) {
        if (roleService.save(PmsRole)) {
            return Result.OK("添加成功");
        }
        return Result.error("添加失败");
    }

    @Operation(summary = "修改角色")
    @PutMapping()
    public Result<?> update(@RequestBody PmsRole PmsRole) {
        if (roleService.updateById(PmsRole)) {
            return Result.OK("修改成功");
        }
        return Result.error("修改失败");
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable("id") Long id) {
        if (roleService.removeById(id)) {
            return Result.OK("删除成功");
        }
        return Result.error("删除失败");
    }

	/**
	 * 复制角色
	 */
	@Operation(summary = "复制角色")
	@GetMapping("/copy/{id}")
	public Result<?> copy(@PathVariable("id") Long id) {
		PmsRole role = roleService.getById(id);
		role.setId(null);
		role.setUid(null);
		if (roleService.save(role)) {
			return Result.OK("复制成功");
		}
		return Result.error("复制失败");
	}

	/**
	 * 清除角色
	 */
	@Operation(summary = "清除角色")
	@GetMapping("/clean/{id}")
	public Result<?> clean(@PathVariable("id") Long id) {
		PmsRole role = roleService.getById(id);
		role.setUid(null);
		if (roleService.updateById(role)) {
			return Result.OK("复制成功");
		}
		return Result.error("复制失败");
	}

}
